package SHNOPData
{
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.messaging.ChannelSet;
    import mx.rpc.AsyncToken;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;
    
	public class AuthModule
	{
		private var remoteObject:RemoteObject;
		
		public var onReceive_CheckLogIn:Function;
		public var onReceive_ChangeUserPassword:Function;
		public var onErrorFunction:Function;
		
		public function AuthModule()
		{
		    remoteObject  = new RemoteObject("fluorine");
		    remoteObject.source = "SHNOPData.AuthModule";
		    remoteObject.DoCheckLogIn.addEventListener("result",DoCheckLogIn_ResultHandler);
		    remoteObject.ChangeUserPassword.addEventListener("result",ChangeUserPassword_ResultHandler);
		    remoteObject.addEventListener("fault", onFault);

			onReceive_CheckLogIn = null;
			onReceive_ChangeUserPassword = null;
			onErrorFunction = null;
		}
		  
		public function setCredentials( userid:String, password:String ):void
		{
			remoteObject.logout();
		    remoteObject.setCredentials( userid, password );
		}
		  //====================
		public function DoCheckLogIn(uname:String,upwd:String, responder:IResponder = null ):void
		{
		    var asyncToken:AsyncToken = remoteObject.DoCheckLogIn(uname,upwd);
		    
		    if( responder != null )
		        asyncToken.addResponder(responder);
		} 
		public function DoCheckLogIn_ResultHandler(event:ResultEvent):void
		{
			if(null != onReceive_CheckLogIn)
				onReceive_CheckLogIn(event.result);
		}
		  //====================
		public function ChangeUserPassword(uname:String,oldpwd:String,newpwd:String, responder:IResponder = null ):void
		{
		    var asyncToken:AsyncToken = remoteObject.ChangeUserPassword(uname,oldpwd,newpwd);
		    
		    if( responder != null )
		        asyncToken.addResponder(responder);
		} 
		public function ChangeUserPassword_ResultHandler(event:ResultEvent):void
		{
			if(null != onReceive_ChangeUserPassword)
				onReceive_ChangeUserPassword(event.result);
		}
		
		//=========================
		public function onFault (event:FaultEvent):void
		{
			if(onErrorFunction!=null)
			{
				onErrorFunction();
			}
			else
			{
		    	Alert.show(event.fault.faultString, "Error");
			}
		}
	}
}
  